package graphique;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import modele.Event;

import executer.Controleur;



public class Fenetre extends JFrame{

	private static final long serialVersionUID = 1L;
	private Controleur control;
	private ListContacts panRep;
	private JPanel panRep1;
	private DetailContact panDetailRep;
	private int longueurFen= 600;
    private int largeurFen = 750;
    private JTabbedPane onglet;
    private JSplitPane splitCal;
	private JSplitPane splitRep;
	private Calendrier calend;
    private DetailEvent panDetailCal;
    
	private JButton bouton = new JButton("Ajouter evenement");

	public int getLargeurFen(){
		return largeurFen;
	}
	
	public int getLongueurFen(){
		return longueurFen;
	}
	
	public Fenetre(Controleur control){
		super();
		this.control = control;
		//titre de la fenetre
		setTitle("Agenda Electronique");
		//taille de la fenetre
        setSize(largeurFen,longueurFen);
        //place la fenetre au centre de l'ecran
        setLocationRelativeTo(null);
        //termine le processus lorsque l'on clic sur la croix
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //couleur du fond
        //pan.setBackground(Color.white);
       
        creaFenPrincipale();	
        
  		//this.setVisible(true);

        //creation de la barre de menu
        BarreMenu bM = new BarreMenu(control);
        setJMenuBar(bM.CreationBarMenu());
        setVisible(true);
             
	}
	
	
	public void creaFenPrincipale(){
		//creation de l'objet onglet
		
		onglet = new JTabbedPane();
		
		//creation des panneaux de l'onglet calendrier
		calend = new Calendrier(this);
		panDetailCal = new DetailEvent();
		Dimension preferredSize2 = new Dimension(getWidth(),getHeight());
		panDetailCal.setPreferredSize(preferredSize2);
		this.add(calend);
		this.add(panDetailCal);
		
		//creation du separateur
		splitCal = new JSplitPane(JSplitPane.VERTICAL_SPLIT, calend, panDetailCal);
		splitCal.setDividerLocation(240);
		
		//creation des panneaux de l'onglet repertoire
		//JPanel panRep = new JPanel();
		
		panRep = new ListContacts(control.getAllContacts());
		Dimension preferredSize3 = new Dimension(getWidth(),getHeight());
		panRep.setPreferredSize(preferredSize3);	
		//JPanel panDetailRep = new JPanel();
		panDetailRep = new DetailContact();
		//panDetailRep.add(exporter);
		
		//Dimension preferredSize4 = new Dimension(getWidth(),getHeight());
		//panDetailCal.setPreferredSize(preferredSize4);
		
		
		
		panRep.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
			    int index = panRep.getSelectedIndex();
			    String selected = panRep.get(index);
			    String key = selected.split("\\|")[0].trim();
			    panDetailRep.afficherDetail(panRep.getContact(key));
			}
		});
		
		
		
        this.add(panDetailRep);	
		this.add(panRep);
		
		
		//creation du separateur
		splitRep = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panRep, panDetailRep);
		splitRep.setDividerLocation(300);
		
		//ajout des panneaux a l'objet onglet
		onglet.add("Calendrier", splitCal);
		onglet.add("Repertoire", splitRep);
		//on passe ensuite les onglets au contentPane

		getContentPane().add(onglet);
		setVisible(true);
	}

    void afficherDetail(String s) {
	final String date = s;
        Event ob[] = control.getEventsByDate(s);
        System.out.println("la taille du tableau est "+ob.length);
        panDetailCal.showDetail(ob);
        bouton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent event){
    			new FormulaireEvenement("Ajouter un evenement",date,control);
    		}
    	});
        panDetailCal.add(bouton);
        repaint();
    }
    
    
}
